package com.xxx.article.controller;

import com.xxx.article.domain.Comment;
import com.xxx.article.service.CommentService;
import com.xxx.entity.Result;
import com.xxx.entity.StatusCode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author blijojo
 * @date 2021/6/14
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;


    @GetMapping()
    public Result findAll() {
        List<Comment> list = commentService.findAll();
        return new Result(StatusCode.OK, true, "查询成功", list);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        Comment comment = commentService.findById(id);
        return new Result(StatusCode.OK, true, "查询成功", comment);
    }

    @PostMapping()
    public Result save(@RequestBody Comment comment) {
        commentService.save(comment);
        return new Result(StatusCode.OK, true, "新增成功", null);
    }

    @PutMapping("/{id}")
    public Result updateById(@PathVariable("id") String id, @RequestBody Map<String, Object> map) {
        commentService.updateById(id, map);
        return new Result(StatusCode.OK, true, "修改成功", null);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        commentService.delete(id);
        return new Result(StatusCode.OK, true, "删除成功", null);
    }

    @GetMapping("/article/{articleId}")
    public Result findByArticleId(@PathVariable("articleId") String articleId) {
        List<Comment> list = commentService.findByArticleId(articleId);
        return new Result(StatusCode.OK, true, "查询成功", list);
    }

    /**
     * 点赞
     * 实现点赞与取消点赞
     */
    @PutMapping("/thumbup/{commentId}")
    public Result thumbup(@PathVariable("commentId") String commentId) {
        //模拟获取用户id
        String userId = "1";
        //flag=true: 点赞成功
        //flag=false:取消点赞成功
        boolean flag = commentService.thumbup(commentId, userId);
        if (flag) {
            return new Result(StatusCode.OK, true, "点赞成功", null);
        } else {
            return new Result(StatusCode.OK, true, "取消点赞成功", null);
        }
    }


}
